﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Main_Reports_ServiceInvoice_InvoiceViews_CreditNotes : System.Web.UI.Page
{

    Datautility DU = new Datautility();
    AccountSettings acntstng = new AccountSettings();
    DataTable dtMain = new DataTable();
    string finyear = "";
    Double totalGross = 0;
    Double totalTaxable = 0;
    Double totalTDSAmount = 0;
    Double totalWriteOffAmount = 0;
    Double totalDiscountAmount = 0;
    Double totalReiumbersment = 0;
    Double totalServiceTax = 0;
    Double totalPECess = 0;
    Double totalHECess = 0;

    Double totalGrossN = 0;
    Double totalTaxableN = 0;
    Double totalTDSAmountN = 0;
    Double totalWriteOffAmountN = 0;
    Double totalDiscountAmountN = 0;
    Double totalReiumbersmentN = 0;
    Double totalServiceTaxN = 0;
    Double totalPECessN = 0;
    Double totalHECessN = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            finyear = Session["finyear"].ToString();
            hidfinyear.Value = Session["finyear"].ToString();
            txtFromDate.Text = Convert.ToString("01/04/" + finyear);
            txtToDate.Text = "31/03/" + (Convert.ToInt32(finyear) + 1).ToString();
            BindGrid(txtFromDate.Text, txtToDate.Text);
            lblCompName.Text = Session["compname"].ToString();
            lblCompName2.Text = Session["compname"].ToString();
        }
    }
    protected void BindGrid(string from, string to)
    {
        if (from != string.Empty && to != string.Empty)
        {
            SqlParameter[] sqlparam = {
                                      new SqlParameter("@Finyear",hidfinyear.Value.ToString()),
                                      new SqlParameter("@FROMDATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(from))),
                                      new SqlParameter("@TODATE",string.Format("{0:MM/dd/yyyy}",Convert.ToDateTime(to))),
                                  };

            dtMain = DU.getdatausingprocedure("USP_IVC_GetAllBillwiseCreditnoteDetails", sqlparam);
        }
        lblMsg.Text = "Credit Notes Register : From " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(from)) + " To " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(to)) + ".";
        lblMsg2.Text = "Credit Notes Register : From " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(from)) + " To " + string.Format("{0:dd-MMM-yyyy}",Convert.ToDateTime(to)) + ".";
        grdCreditNoteView.DataSource = dtMain;
        grdCreditNoteView.DataBind();
        grdPrint.DataSource = dtMain;
        grdPrint.DataBind();
    }
    protected void grdCreditNoteView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Double TaxableAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TaxableAmount"));
            Double ReimbursementAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReimbursementAmount"));
            Double TdsAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TDSAmount"));
            Double WriteOffAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "WriteOffAmount"));
            Double DiscountAmount = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "DiscountAmount"));
            Double ServiceTax1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ServiceTax1"));
            Double PECess1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "PECess1"));
            Double HECess1 = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "HECess1"));
            Double Gross = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Gross"));

            totalTaxable = totalTaxable + Math.Abs(TaxableAmount);
            totalTDSAmount = totalTDSAmount + Math.Abs(TdsAmount);
            totalDiscountAmount = totalDiscountAmount + Math.Abs(DiscountAmount);
            totalWriteOffAmount = totalWriteOffAmount + Math.Abs(WriteOffAmount);
            totalHECess = totalHECess + Math.Abs(HECess1);
            totalPECess = totalPECess + Math.Abs(PECess1);
            totalReiumbersment = totalReiumbersment + Math.Abs(ReimbursementAmount);
            totalServiceTax = totalServiceTax + Math.Abs(ServiceTax1);
            totalGross = totalGross + Math.Abs(Gross);

            if (Math.Abs(TaxableAmount) > 0)
            {
                e.Row.Cells[4].Text = acntstng.GetCurrency(Math.Abs(TaxableAmount));
            }
            else
            {
                e.Row.Cells[4].Text = string.Empty;
            }
            
            if (Math.Abs(ReimbursementAmount) > 0)
            {
                e.Row.Cells[5].Text = acntstng.GetCurrency(Math.Abs(ReimbursementAmount));
            }
            else
            {
                e.Row.Cells[5].Text = string.Empty;
            }
            if (Math.Abs(TdsAmount) > 0)
            {
                e.Row.Cells[6].Text = acntstng.GetCurrency(Math.Abs(TdsAmount));
            }
            else
            {
                e.Row.Cells[6].Text = string.Empty;
            }
            if (Math.Abs(WriteOffAmount) > 0)
            {
                e.Row.Cells[7].Text = acntstng.GetCurrency(Math.Abs(WriteOffAmount));
            }
            else
            {
                e.Row.Cells[7].Text = string.Empty;
            }
            if (Math.Abs(DiscountAmount) > 0)
            {
                e.Row.Cells[8].Text = acntstng.GetCurrency(Math.Abs(DiscountAmount));
            }
            else
            {
                e.Row.Cells[8].Text = string.Empty;
            }
            if (Math.Abs(ServiceTax1) > 0)
            {
                e.Row.Cells[9].Text = acntstng.GetCurrency(Math.Abs(ServiceTax1));
            }
            else
            {
                e.Row.Cells[9].Text = string.Empty;
            }
            if (Math.Abs(PECess1) > 0)
            {
                e.Row.Cells[10].Text = acntstng.GetCurrency(Math.Abs(PECess1));
            }
            else
            {
                e.Row.Cells[10].Text = string.Empty;
            }
            if (Math.Abs(HECess1) > 0)
            {
                e.Row.Cells[11].Text = acntstng.GetCurrency(Math.Abs(HECess1));
            }
            else
            {
                e.Row.Cells[11].Text = string.Empty;
            }
            if (Math.Abs(Gross) > 0)
            {
                e.Row.Cells[12].Text = acntstng.GetCurrency(Math.Abs(Gross));
            }
            else
            {
                e.Row.Cells[12].Text = string.Empty;
            }

            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[7].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[8].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[9].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[10].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[11].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[12].HorizontalAlign = HorizontalAlign.Right;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[3].Text = "Total";
            e.Row.Cells[4].Text = acntstng.GetCurrency(totalTaxable).ToString();
            e.Row.Cells[5].Text = acntstng.GetCurrency(totalReiumbersment).ToString();
            e.Row.Cells[6].Text = acntstng.GetCurrency(totalTDSAmount).ToString();
            e.Row.Cells[7].Text = acntstng.GetCurrency(totalWriteOffAmount).ToString();
            e.Row.Cells[8].Text = acntstng.GetCurrency(totalDiscountAmount).ToString();
            e.Row.Cells[9].Text = acntstng.GetCurrency(totalServiceTax).ToString();
            e.Row.Cells[10].Text = acntstng.GetCurrency(totalPECess).ToString();
            e.Row.Cells[11].Text = acntstng.GetCurrency(totalHECess).ToString();
            e.Row.Cells[12].Text = acntstng.GetCurrency(totalGross).ToString();
            e.Row.Font.Bold = true;
            e.Row.HorizontalAlign = HorizontalAlign.Right;
            e.Row.VerticalAlign = VerticalAlign.Bottom;
        }

    }
    protected void btnCangeDate_Click(object sender, EventArgs e)
    {
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }
    protected void grdCreditNoteView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCreditNoteView.PageIndex = e.NewPageIndex;
        BindGrid(txtFromDate.Text, txtToDate.Text);
    }
    protected void grdPrint_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Double TaxableAmountN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TaxableAmount"));
            Double ReimbursementAmountN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ReimbursementAmount"));
            Double TdsAmountN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TDSAmount"));
            Double WriteOffAmountN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "WriteOffAmount"));
            Double DiscountAmountN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "DiscountAmount"));
            Double ServiceTax1N = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ServiceTax1"));
            Double PECess1N = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "PECess1"));
            Double HECess1N = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "HECess1"));
            Double GrossN = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "Gross"));
            Double Tax = ServiceTax1N + PECess1N + HECess1N;
            Double NetAmt = Tax + GrossN;

            totalTaxableN = totalTaxableN + Math.Abs(TaxableAmountN);
            totalTDSAmountN = totalTDSAmountN + Math.Abs(TdsAmountN);
            totalDiscountAmountN = totalDiscountAmountN + Math.Abs(DiscountAmountN);
            totalWriteOffAmountN = totalWriteOffAmountN + Math.Abs(WriteOffAmountN);
            totalHECessN = totalHECessN + Math.Abs(HECess1N);
            totalPECessN = totalPECessN + Math.Abs(PECess1N);
            totalReiumbersmentN = totalReiumbersmentN + Math.Abs(ReimbursementAmountN);
            totalServiceTaxN = totalServiceTaxN + Math.Abs(ServiceTax1N);
            totalGrossN = totalGrossN + Math.Abs(GrossN);

            if (Math.Abs(TaxableAmountN) > 0)
            {
                e.Row.Cells[2].Text = acntstng.GetCurrency(Math.Abs(TaxableAmountN));
            }
            else
            {
                e.Row.Cells[2].Text = string.Empty;
            }

            if (Math.Abs(ReimbursementAmountN) > 0)
            {
                e.Row.Cells[3].Text = acntstng.GetCurrency(Math.Abs(ReimbursementAmountN));
            }
            else
            {
                e.Row.Cells[3].Text = string.Empty;
            }

            if (Math.Abs(TdsAmountN) > 0)
            {
                e.Row.Cells[4].Text = acntstng.GetCurrency(Math.Abs(TdsAmountN));
            }
            else
            {
                e.Row.Cells[4].Text = string.Empty;
            }
            if (Math.Abs(WriteOffAmountN) > 0)
            {
                e.Row.Cells[5].Text = acntstng.GetCurrency(Math.Abs(WriteOffAmountN));
            }
            else
            {
                e.Row.Cells[5].Text = string.Empty;
            }
            if (Math.Abs(DiscountAmountN) > 0)
            {
                e.Row.Cells[6].Text = acntstng.GetCurrency(Math.Abs(DiscountAmountN));
            }
            else
            {
                e.Row.Cells[6].Text = string.Empty;
            }
            if (Math.Abs(Tax) > 0)
            {
                e.Row.Cells[7].Text = acntstng.GetCurrency(Math.Abs(Tax));
            }
            else
            {
                e.Row.Cells[7].Text = string.Empty;
            }
            if (Math.Abs(NetAmt) > 0)
            {
                e.Row.Cells[8].Text = acntstng.GetCurrency(Math.Abs(NetAmt));
            }
            else
            {
                e.Row.Cells[8].Text = string.Empty;
            }

            e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[4].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[5].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[6].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[7].HorizontalAlign = HorizontalAlign.Right;
            e.Row.Cells[8].HorizontalAlign = HorizontalAlign.Right;
           
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Double totalTax = totalHECessN + totalPECessN + totalServiceTaxN;
            Double totalAmt = totalTax + totalTaxableN + totalReiumbersmentN;
            e.Row.Cells[1].Text = "Total";
            e.Row.Cells[2].Text = acntstng.GetCurrency(totalTaxableN).ToString();
            e.Row.Cells[3].Text = acntstng.GetCurrency(totalReiumbersmentN).ToString();
            e.Row.Cells[4].Text = acntstng.GetCurrency(totalTDSAmountN).ToString();
            e.Row.Cells[5].Text = acntstng.GetCurrency(totalWriteOffAmountN).ToString();
            e.Row.Cells[6].Text = acntstng.GetCurrency(totalDiscountAmountN).ToString();
            e.Row.Cells[7].Text = acntstng.GetCurrency(totalTax).ToString();
            e.Row.Cells[8].Text = acntstng.GetCurrency(totalAmt).ToString();
            e.Row.Font.Bold = true;
            e.Row.HorizontalAlign = HorizontalAlign.Right;
            e.Row.VerticalAlign = VerticalAlign.Bottom;
        }

    }
}
